#pragma once

#include "Hash.h"

namespace byh
{
	template<class K, class V, class HashIFunc = hash_bucket::Integer<K>>
	class unordered_map
	{
	public:
		struct KeyOfMap
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

		typedef typename hash_bucket::HashTable<K, pair<const K, V>, KeyOfMap, HashIFunc>::Iterator iterator;
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, KeyOfMap, HashIFunc>::const_Iterator const_iterator;

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.insert(kv);
		}

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}

	protected:
		hash_bucket::HashTable<K, pair<const K, V>, KeyOfMap, HashIFunc> _ht;
	};

	void Print(const unordered_map<string, int>& hash)
	{
		unordered_map<string, int>::const_iterator it = hash.begin();
		while (it != hash.end())
		{
			//it->first += 'a';
			//it->second += 1;
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}

	void Test_unordered_map()
	{
		//unordered_map<int, int> hash;
		//hash.insert({ 3,3 });
		//hash.insert({ 2,2 });
		//hash.insert({ 1,1 });
		//hash.insert({ 4,4 });

		//unordered_map<int, int>::iterator it = hash.begin();
		//while (it != hash.end())
		//{
		//	cout << it->first << ":" << it->second << endl;
		//	++it;
		//}
		//cout << endl;
		
		unordered_map<string, int> hash;
		vector<string> v = { "apple", "watermelon", "strawberry", "orange", "apple", "apple" };
		for (auto e : v)
			hash[e]++;
		Print(hash);
	}
}
